﻿using System;
using System.Collections.Generic;
using Epitech.Krisk.Xna.ScreenManagerLib.ScreenManager;
using Epitech.Krisk.Xna.ScreenManagerLib.Widget;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Epitech.Krisk.Xna.UI.Game.Utils
{
    public class InfoMessage : MenuScreen
    {
        private Texture2D _popup;
        private Texture2D _blank;
        private String _msg;
        private List<String> _msgs;
        private Vector2 _pos;
        private MenuEntry _ok;
        private LayoutEntry _button;

        public InfoMessage(string msg) : base("")
        {
            _msg = msg;
        }

        public override void LoadContent()
        {
            _popup = ScreenManager.Game.Content.Load<Texture2D>("Utils/Popup");
            _blank = ScreenManager.Game.Content.Load<Texture2D>("blank");
            _ok = new MenuEntry("[ OK ]")
                      {SelectedColor = new Color(119, 121, 81), DefaultColor = new Color(208, 44, 6), IsPulse = false};
            _ok.Selected += OkSelected;



            _pos = new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2, ScreenManager.GraphicsDevice.Viewport.Height / 2);
            _pos.X -= _popup.Width / 2;
            _pos.Y -= _popup.Height / 2;

            _msgs = new List<string>();
            foreach (var line in _msg.Split('\n'))
            {
                _msgs.Add(line);
            }

            _button =
                new LayoutEntry(new Vector2(ScreenManager.GraphicsDevice.Viewport.Width/2, _pos.Y + _popup.Height - 100))
                    {Alignement = LayoutEntry.TextAlignement.Center};
            _button.AddEntry(_ok); MenuEntries.Add(_button);
            base.LoadContent();
        }

        private void OkSelected(object sender, PlayerIndexEventArgs e)
        {
            Sound.GetInstance().setMusic("Music/ButtonSound");
            ScreenManager.RemoveScreen(this);
        }

        public override void Draw(GameTime gameTime)
        {
            Rectangle area = ScreenManager.GraphicsDevice.Viewport.Bounds;

            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(_blank, area, Color.FromNonPremultiplied(0, 0, 0, 128));
            ScreenManager.SpriteBatch.Draw(_popup, _pos, Color.White);
            float spacing = 0;
            foreach (var msg in _msgs)
            {
                Vector2 pos = new Vector2(_popup.Width / 2 - ScreenManager.Font.MeasureString(msg).X / 2, 50);
                pos += _pos;
                pos.Y += spacing;
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, msg, pos, Color.Black);
                spacing = ScreenManager.Font.MeasureString(msg).Y - 25;
            }
            ScreenManager.SpriteBatch.End();
            base.Draw(gameTime);
        }

    }
}
